//
//  NNMockQrViewController.m
//  YHB_Prj
//
//  Created by fangshufeng on 2018/1/16.
//  Copyright © 2018年 striveliu. All rights reserved.
//

#import "NNMockQrViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "NNMockScanView.h"

@interface NNMockQrViewController ()
<AVCaptureMetadataOutputObjectsDelegate,
UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
NNMockScanViewDelete>

@property (nonatomic, strong) AVCaptureSession *session;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@property (nonatomic, strong) NNMockScanView *scanView;

@end

@implementation NNMockQrViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self configSelf];
    [self configSubviews];
}

- (void)configSelf {
    
    self.view.backgroundColor = [UIColor clearColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupNavigationBar];
}

- (void)configSubviews {
    
    _scanView = [[NNMockScanView alloc] init];
    [_scanView setDelegate:self];
    [self.view addSubview:_scanView];
    [_scanView setFrame:self.view.bounds];
    
    [self setupSGQRCodeScanning];
}

- (void)viewWillLayoutSubviews {
    
    [super viewWillLayoutSubviews];
    [_scanView setFrame:self.view.bounds];
}

- (void)setupNavigationBar {
    self.navigationItem.title = @"扫一扫";
}

#pragma mark - - - 从相册中识别二维码, 并进行界面跳转

- (void)setupSGQRCodeScanning {
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 设置扫描范围(每一个取值0～1，以屏幕右上角为坐标原点)
    // 注：微信二维码的扫描范围是整个屏幕，这里并没有做处理（可不用设置）
    output.rectOfInterest = CGRectMake(0.05, 0.2, 0.7, 0.6);
    
    self.session = [[AVCaptureSession alloc] init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    [_session addInput:input];
    [_session addOutput:output];
    
    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,
                                   AVMetadataObjectTypeEAN13Code,
                                   AVMetadataObjectTypeEAN8Code,
                                   AVMetadataObjectTypeCode128Code];
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _previewLayer.frame = self.view.layer.bounds;
    [self.view.layer insertSublayer:_previewLayer atIndex:0];
    
    [_session startRunning];
}

#pragma mark - - - AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
       fromConnection:(AVCaptureConnection *)connection {
    
    [self.session stopRunning];
    [self.previewLayer removeFromSuperlayer];
    if (metadataObjects.count > 0) {
        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
        
        if ([_delegate respondsToSelector:@selector(qrScanVc:didCatchQRCodeString:)]) {
            [_delegate qrScanVc:self didCatchQRCodeString:obj.stringValue];
        }
    }
}

#pragma mark - MZLogFloaterQRCodeScanViewDelegate

- (void)scanViewReturnButtonCLicked:(NNMockScanView *)scanView {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
